/**
 * Date:2021年9月10日 上午10:50:15
 * Copyright (c) 2021, www.bwbroad.com All Rights Reserved
 */

package org.mengyun.tcctransaction.sample.config;

import javax.sql.DataSource;

import org.mengyun.tcctransaction.recovery.DefaultRecoverFrequency;
import org.mengyun.tcctransaction.repository.JdbcTransactionRepository;
import org.mengyun.tcctransaction.serializer.JacksonTransactionSerializer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * Description: TODO <br/>
 * Date:2021年9月10日 上午10:50:15 <br/>
 * 
 * @author xuejianxin
 */

@Configuration
public class TccTransactionConfig {

	@Primary
	@Bean(name = "datasource")
	@ConfigurationProperties(prefix = "spring.datasource")
	public DataSource datasource() {
		return DataSourceBuilder.create().build();
	}

	@Bean(name = "datasourceTcc")
	@ConfigurationProperties(prefix = "spring.datasource-tcc")
	public DataSource datasourceTcc() {
		return DataSourceBuilder.create().build();
	}

//	
//	<bean id="transactionRepository"
//		      class="org.mengyun.tcctransaction.spring.repository.JdbcTransactionRepository">
//		    <property name="dataSource" ref="dataSource"/>
//		    <property name="domain" value="TCC:HTTP:CAPITAL:"/>
//    </bean>

	@Bean("transactionRepository")
	public JdbcTransactionRepository transactionRepository() {
		JdbcTransactionRepository r = new JdbcTransactionRepository();
		r.setDataSource(datasourceTcc());
		r.setDomain("TCC:HTTP:CREDIT:");
		r.setSerializer(new JacksonTransactionSerializer());
		return r;
	}

//<bean id="recoverFrequency" class="org.mengyun.tcctransaction.recovery.DefaultRecoverFrequency">
//    <property name="maxRetryCount" value="30"/>
//    <property name="recoverDuration" value="30"/>
//    <property name="cronExpression" value="0/15 * * * * ?"/>
//    <property name="fetchPageSize" value="200"/>
//</bean>

	@Bean("recoverFrequency")
	public DefaultRecoverFrequency recoverFrequency() {
		DefaultRecoverFrequency r = new DefaultRecoverFrequency();
		r.setMaxRetryCount(30);
		r.setRecoverDuration(30);
		r.setCronExpression("0/15 * * * * ?");
		r.setFetchPageSize(200);
		return r;
	}

}
